package cn.pomelo.secondskill.rabbitMQ;

import cn.pomelo.secondskill.rabbitMQ.domain.MessageEvent;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * RabbitMQ 通用的消息发送服务
 *
 * @param <T>
 */
@Service
public class Subscriber<T> {

    @Resource
    private RabbitTemplate rabbitTemplate;

    public void send(String routingKey, String message, T t) {
        if (!Objects.isNull(t)) {
            MessageEvent event = new MessageEvent();
            event.setMessage(message);
            event.setData(t);
            rabbitTemplate.convertAndSend(routingKey, event);
        }
    }
}
